﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataManagement.Interfaces;
using System.Collections;
using Microsoft.SqlServer.Management.Smo;
using ICeFramework.DataManagement.MSSql;

namespace ICeFramework.DataManagement.Collections
{
    public class SqlDatabaseList
    {
        private List<IDatabase> _listInternal;
        private Server _server;
        public Server Server
        {
            get { return _server; }
        }

        public IDatabase this[int index]
        {
            get
            {
                IDatabase db = _listInternal[index];
                db.Tables.Clear();
                Database srvDb = _server.Databases[db.Name];
                foreach (Table srvTbl in srvDb.Tables)
                {
                    MSSqlTable tbl = new MSSqlTable(srvTbl);
                    db.Tables.Add(tbl);
                }
                return db;
            }
        }

        public SqlDatabaseList(Server server)
        {
            _listInternal = new List<IDatabase>();
            _server = server;
        }

        public void Add(IDatabase database)
        {
            _listInternal.Add(database);
        }

        public void Clear()
        {
            _listInternal.Clear();
        }

        public bool Contains(IDatabase item)
        {
            return _listInternal.Contains(item);
        }

        public int Count
        {
            get { return _listInternal.Count; }
        }

        public bool Remove(IDatabase item)
        {
            return _listInternal.Remove(item);
        }

        public IEnumerator<IDatabase> GetEnumerator()
        {
            return _listInternal.GetEnumerator();
        }
    }
}
